/**
 * 
 */
package com.wisdytech.linkdcs.equipment.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.wisdytech.common.handler.ResourceMatchingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.session.UserSession;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.equipment.dto.EquipConnectDTO;
import com.wisdytech.linkdcs.equipment.model.EquipmentToClient;
import com.wisdytech.linkdcs.equipment.service.IEquipmentConnectService;
import com.wisdytech.linkdcs.equipment.service.IEquipmentRegisteredService;
import com.wisdytech.linkdcs.equipment.vo.EquipmentConnectVO;
import com.wisdytech.linkdcs.system.model.SysUser;

/**
 * <p>
 * ClassName: 
 * </p>
 * <p>
 * Description: 
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-9-30
 * </p>
 */
@Controller
@RequestMapping("/equipment/connect")
public class EquipmentConnectAction {
    /**
     * 注入equipmentRegisterService
     */
    @Autowired
    @Qualifier("equipmentConnectService")
    private IEquipmentConnectService equipmentConnectService;
    /**
     * 注入equipmentRegisterService
     */
    @Autowired
    @Qualifier("equipmentRegisterService")
    private IEquipmentRegisteredService equipmentRegisterService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    
    @RequestMapping("/index")
    public String index(Model model,String clientCode, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        model.addAttribute("clientCode",clientCode);
        return "equipment/collectTerminal/connectEquipment";
    }
    
    /**
     * <p>Description: 保存采集终端和设备连接信息</p> 
     * @param clientConnect 设备连接前台对象信息
     * @return 处理结果对象
     */
    @RequestMapping(value = "/saveEquipmentConnect/save.mvc")
    @ResponseBody
    public StandardResult saveEquipmentConnect(EquipmentToClient clientConnect, HttpSession session) {
        SysUser userSession = BaseUtil.getCurrentUser();
        String loginName = userSession.getUsername();
        if(StringUtils.isBlank(clientConnect.getEquipCode())
        		|| StringUtils.isBlank(clientConnect.getClientId())) {
        	return StandardResult.getFailResult();
        }
        //新增和修改
        if(StringUtils.isNotBlank(clientConnect.getPkId())) {
        	//修改
        	return equipmentConnectService.updateEquipmentConnect(clientConnect, loginName);
        }else {
        	//新增
        	return equipmentConnectService.addEquipmentConnect(clientConnect, loginName);
        	
        }
        
        
       
    }
    
    /**
     * 分页条件查询连接设备页面信息
     * 
     * @param clientCode 终端编号
     * @param filter 分页参数
     * @return 分页查询结果集
     */
    @RequestMapping(value = "/listEquipmentConnectByFilter/get.mvc")
    @ResponseBody
    public StandardTableResult<EquipmentConnectVO> listEquipmentConnectByFilter(EquipConnectDTO connectDTO, Filter filter) {
       PageInfo<EquipmentToClient> equipmentConnects = equipmentConnectService.listEquipmentConnectByFilter(connectDTO, filter);
       
       
       //转换为前台结果集
       List<EquipmentToClient> connectList = equipmentConnects.getList();
       List<EquipmentConnectVO> connectVOList=new ArrayList<EquipmentConnectVO>();
       for(EquipmentToClient conn: connectList){
           connectVOList.add(DataTransformUtil.changeEquipmentConnectVO(conn));
       }
       return new StandardTableResult<EquipmentConnectVO>(equipmentConnects.getTotal(),connectVOList);
    }
    
    /**
     * 通过主键id查询对应的连接设备信息
     * @param pkId 主键id
     * @return 连接设备信息
     */
    @RequestMapping("/getEquipmentCollectById/get.mvc")
    @ResponseBody
    public EquipmentConnectVO getEquipmentCollectById(String pkId){
        EquipmentConnectVO connectVO = new EquipmentConnectVO();
        connectVO = equipmentConnectService.getEquipmentCollectById(pkId);
        return connectVO;
    }
    /**
     * 通过主键id删除对应的采集终端下连接的设备信息
     * @param pkIds 主键id
     * @return 结果对象
     */
    @RequestMapping("/delEquipmentConnect/del.mvc")
    @ResponseBody
    public StandardResult delEquipmentConnect(String pkIds) {
    	
    	List<String> pkIdLists=
    			JSON.parseArray(pkIds, String.class);
    	return equipmentConnectService.delEquipmentConnect(pkIdLists);
    }
    
}
